package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.enums.IgasYesOrNoEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.igas.dao.PBankSupportCfgMapper;
import cc.rengu.redp.bizimpl.igas.domain.PBankSupportCfg;
import cc.rengu.redp.bizimpl.igas.domain.PDstChannelInfo;
import cc.rengu.redp.bizimpl.igas.service.PBankSupportCfgService;
import cc.rengu.redp.bizimpl.igas.service.PDstChannelInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysSeqService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 刘玉亮.LL
 */
@Service
@DS("igas")
public class PBankSupportCfgServiceImpl extends ServiceImpl<PBankSupportCfgMapper, PBankSupportCfg> implements PBankSupportCfgService {


    @Autowired
    IUpmpSysParamService upmpSysParamService;
    @Autowired
    private PDstChannelInfoService pDstChannelInfoService;
    @Autowired
    private IUpmpSysSeqService upmpSysSeqService;

    @Override
    public IPage<PBankSupportCfg> findByPage(QueryRequest request, PBankSupportCfg info) {
        try {
            LambdaQueryWrapper<PBankSupportCfg> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getInstId())) {
                queryWrapper.eq(PBankSupportCfg::getInstId, info.getInstId());
            } else {
                //获取当前用户的法人id
                UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
                queryWrapper.eq(PBankSupportCfg::getInstId, user.getInstId());
            }
            if (StringUtils.isNotBlank(info.getDstChannelId())) {
                queryWrapper.likeRight(PBankSupportCfg::getDstChannelId, info.getDstChannelId());
            }
            if (StringUtils.isNotBlank(info.getSupportBizType())) {
                queryWrapper.likeRight(PBankSupportCfg::getSupportBizType, info.getSupportBizType());
            }
            if (StringUtils.isNotBlank(info.getBankNo())) {
                queryWrapper.likeRight(PBankSupportCfg::getBankNo, info.getBankNo());
            }
            queryWrapper.orderByDesc(PBankSupportCfg::getBankNo);
            Page<PBankSupportCfg> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    @Override
    public PBankSupportCfg editFromChnl(PDstChannelInfo pDstChannelInfo) throws RedpException {
        UpmpUserInfoVO user=CommonUtils.getCurrentUser(upmpSysParamService);
        if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(pDstChannelInfo.getOptFlag())) {
            log.error("不支持审核操作!");
            throw new RedpException("不支持审核操作！");
        } else if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(pDstChannelInfo.getOptFlag())
                || PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(pDstChannelInfo.getOptFlag())) {
            if (Common.isNotBlank(pDstChannelInfo.getSecPlanId())
                    && pDstChannelInfo.getSecPlanId().startsWith(pDstChannelInfo.getDstChannelId())) {
            } else {
                pDstChannelInfo.setSecPlanId(genSecPlanId(pDstChannelInfo.getDstChannelId()));
            }
            pDstChannelInfoService.update(pDstChannelInfo); //这是什么骚操作
        }
        PBankSupportCfg cfg = null;
        if (!PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(pDstChannelInfo.getOptFlag())) {
            cfg = this.findOne(pDstChannelInfo.getInstId(), pDstChannelInfo.getDstChannelId(), pDstChannelInfo.getSupportBizType(), pDstChannelInfo.getPBankSupportCfg());
        }
        if (Common.isBlank(cfg)) {
            cfg = new PBankSupportCfg();
            cfg.setInstId(pDstChannelInfo.getInstId());
            cfg.setDstChannelId(pDstChannelInfo.getDstChannelId());
            cfg.setCreditSupportFlag(IgasYesOrNoEnum.Yes.getType());
            cfg.setDebitSupportFlag(IgasYesOrNoEnum.Yes.getType());
            cfg.setMcFlag(IgasYesOrNoEnum.No.getType());
            cfg.setRecordStatus(IgasYesOrNoEnum.Yes.getType());
            cfg.setOptFlag(pDstChannelInfo.getOptFlag());
            cfg.setOprId(user.getId());
            cfg.setRecordStatus(IgasYesOrNoEnum.Yes.getType());
            cfg.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
        }
        cfg.setDstChannelName(pDstChannelInfo.getDstChannelId() + "(" + pDstChannelInfo.getDstChannelName() + ")");
        return cfg;
    }

    @Override
    public PBankSupportCfg findOne(String instId, String dstChannelId, String supportBizType, String bankNo) throws RedpException {
        LambdaQueryWrapper<PBankSupportCfg> wrapper = new LambdaQueryWrapper<>();

        if (Common.isNotBlank(instId)) {
            wrapper.eq(PBankSupportCfg::getInstId, instId);
        } else {
            throw new RedpException("查询支持银行列表配置信息唯一索引列[instId]条件为空");
        }
        if (!Common.isBlank(dstChannelId)) {
            wrapper.eq(PBankSupportCfg::getDstChannelId, dstChannelId);
        } else {
            throw new RedpException("查询支持银行列表配置信息唯一索引列[dstChannelId]条件为空");
        }
        if (!Common.isBlank(supportBizType)) {
            wrapper.eq(PBankSupportCfg::getSupportBizType, supportBizType);
        } else {
            throw new RedpException("查询支持银行列表配置信息唯一索引列[supportBizType]条件为空");
        }
        if (!Common.isBlank(bankNo)) {
            wrapper.eq(PBankSupportCfg::getBankNo, bankNo);
        } else {
            throw new RedpException("查询支持银行列表配置信息唯一索引列[bankNo]条件为空");
        }
        List<PBankSupportCfg> resultList = this.list(wrapper);
        if (!resultList.isEmpty()){
            PBankSupportCfg obj = resultList.get(0);
        if (Common.isNotBlank(obj)) {
//            Map map = (Map) obj;
//            PBankSupportCfg pBankSupportCfg = CommonUtils.parseMapToBean(map, PBankSupportCfg.class);
            return obj;
        }}
        log.trace("没有查询到银行列表配置信息");
        return null;
    }

    /**
     * 安全计划编号，格式为: dstChannelId+8位排序编码
     *
     * @return
     */
    private String genSecPlanId(String dstChannelId) {
        // 固定入参
        String common = "SEC_PLAN_ID";
        String prefix = dstChannelId;

//		dstChannelId+8位排序编码
        // 生成序列号
        String serialNumber = upmpSysSeqService.updateParamValue(common, prefix, "20");
        String result = prefix + String.format("%08d", Integer.parseInt(serialNumber));
        return result;
    }
}
